﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;

namespace VoteUphold
{
    /// <summary>
    /// cookie
    /// </summary>
    public class GetCookie
    {
        //获取 CookieContainer
        /// <summary>
        /// 获取 CookieContainer
        /// </summary>
        /// <param name="cookieStr"></param>
        /// <param name="url"></param>
        /// <returns></returns>
        public static CookieContainer GetCookieContainer(string cookieStr, params Uri[] urls)
        {
            CookieContainer myCookieContainer = new CookieContainer();
            myCookieContainer.PerDomainCapacity = 200;
            foreach (string str in cookieStr.Split(';'))
            {
                string[] cookieNameValue = str.Split('=');
                string value = "";
                for (int i = 1; i < cookieNameValue.Length; i++)//防止值出现 = 符号
                {
                    value += cookieNameValue[i].Trim() + "=";
                }
                value = value.TrimEnd('=');
                Cookie ck = new Cookie(cookieNameValue[0].Trim().ToString(),
                    value.Replace(",", "%2C"));
                foreach (var item in urls)
                {
                    myCookieContainer.Add(item, ck);
                }
            }
            return myCookieContainer;
        }
    }
}
